using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EditWebSite.Database;
using System.Diagnostics;
using EditWebSite.Logics;

namespace EditWebSite.Presentation
{
    internal partial class FrmTabelle : Form
    {
        Dictionary<string, Tabella> _Tabelle;
        DatabaseHelper _Helper;

        public FrmTabelle(DatabaseHelper Helper)
        {
            InitializeComponent();

            _Helper = Helper;
            caricaTabelle();
            
        }

        private void caricaTabelle()
        {
            try
            {
                _Tabelle = _Helper.GetTabelle();

                foreach (string tab in _Tabelle.Keys)
                {
                    lstTabelle.Items.Add(tab);
                }
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }

        private void lstTabelle_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lstwColonne.Items.Clear();

                if (lstTabelle.SelectedIndex > -1)
                {
                    Tabella tab = _Tabelle[lstTabelle.SelectedItem.ToString()];

                    for (int i = 0; i < tab.Colonne.Length; i++)
                    {
                        ListViewItem itm = new ListViewItem(tab.Colonne[i].Nome);
                        itm.SubItems.Add(tab.Colonne[i].Tipo.ToString());
                        itm.Tag = tab.Colonne[i];

                        lstwColonne.Items.Add(itm);
                    }
                }
            }
            catch (Exception ex)
            {
                #region LOGIC
#if DEBUG
                Debugger.Break();
#endif
                Messaggi.Error(ex.Message);
                #endregion
            }
        }
    }
}